/**
 * @file swei_protocol.c
 * @brief 
 * @author  ()
 * @version 1.0
 * @date 2024-09-13
 * 
 * @copyright Copyright (C) 2019 Queclink Wireless Solutions (ShenZhen) Co., Ltd. All Rights Reserved.
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2024-09-13 <td>1.0     <td>     <td>内容
 * </table>
 */
#include "include/port/swei_log.h"
#include "include/swei_protocol.h"

#include "include/port/swei_fw_info.h"
#include "include/port/swei_wrapper.h"
#include "include/swei_package.h"
#include "include/swei_socket.h"
#include "include/swei_login.h"
#include "include/swei_cache.h"

#include "global.h"



//static int32_t s_login_cnt = 0;   /*!< 登录计数，登录成功后清零计数 */
//static int32_t s_heatbeat_cnt = 0;/*!< 心跳发送计数, 心跳发送成功后清零 */
/**
 * @description: 打包登录消息并发送
 * @return {*}
 * @Date: 2023-09-07 19:44:39
 * @modification:
 */
int32_t swei_srv_login(uint8_t retry)
{
    login_msg_st msg_st;
    uint8_t send_login_msg[256];
    uint8_t send_len;
//    int32_t     result = -1;

    send_len = swei_package_protocol_msg(NULL, (uint8_t *)&msg_st, NET_PROTOCOL_ID_LOGSTEP_NEW, send_login_msg);
    if (send_len <= 0)
    {
        log_e("tbox package login fail, send_len = %d", send_len);
        return -EPERM;
    }


#if 0
    /* 无论是否发送成功都要无条启动等待server ack定时器 */
    result = swei_tcp_write((const void *)send_login_msg, send_len);   
    if(result > 0)
    {
        log_i("tbox login protocal send succ, ret:%d", result);
        s_login_cnt++;
        // result = swei_tcp_write_query();
        // if(0 == result)
        // {
        //     log_d("tbox send login msg success!");
        // }
        // else
        // {
        //     log_d("tbox send login msg failed, result = %d!", result);
        //     return -EPERM;
        // }
    }
   else
   {
       log_e("tbox login protocal send fail, ret:%d", result);
       return -EPERM;
   }
#else
    // cache头插发送 TYPE_LOGIN
    swei_datatype_t dt = {0};

    dt.data_ptr = qlmalloc(send_len);
    if (NULL == dt.data_ptr) {
        log_e("login request malloc failed!\r\n");
        return -EPERM;
    }

    memcpy(dt.data_ptr, send_login_msg, send_len);
    dt.data_len = send_len;
    dt.data_type = TYPE_LOGIN;

    if (0 != swei_base_cache_push(&dt)) { 
        log_e("login request push to cache failed!\r\n");
        if (dt.data_ptr) {
            qlfree(dt.data_ptr);
        }
        
        return -EPERM;
    }
#endif
    return SUCC; //SUCCESS;
}



/**
 * @description: 打包发送心跳消息
 * @return {*}
 * @Date: 2023-09-07 19:44:39
 * @modification:
 */
int32_t swei_srv_heartbeat(uint8_t retry)
{
    /* 先设置心跳业务运行标志位 */
    // tbox_power_set_business_flag(HEATBEAT_TYPE, 1);
    /* 发送一个心跳消息,发送失败先不处理等待应答函数超时 */  
    login_msg_st msg_st;
    uint8_t      send_heart_msg[64];
    int32_t      send_len;
//    int32_t          result;
    // uint8_t      run_mode;

    send_len = swei_package_protocol_msg(NULL, (uint8_t *)&msg_st, NET_PROTOCOL_ID_HEART_BEAT, send_heart_msg);
    if (send_len <= 0)
    {
        log_e("tbox package heart msg fail, send_len = %d", send_len);
        return -1;
    }

#if 0    
    result = swei_tcp_write((const void *)send_heart_msg, send_len);
    if(result > 0)
    {
        s_heatbeat_cnt++;
    }
    else
    {
        log_e("tbox send heartbeat msg fail in sleep, retsult = %d", result);
        return result;
    }
#else
    swei_datatype_t dt = {0};

    dt.data_ptr = qlmalloc(send_len);
    if (NULL == dt.data_ptr) {
        log_e("heartbeat request malloc failed!\r\n");
        return -EPERM;
    }
    
    memcpy(dt.data_ptr, send_heart_msg, send_len);
    dt.data_len = send_len;
    dt.data_type = TYPE_HEARTBEAT;

    if (0 != swei_base_cache_push(&dt)) { 
        log_e("heartbeat request push to cache failed!\r\n");
        if (dt.data_ptr) {
            qlfree(dt.data_ptr);
        }
        
        return -EPERM;
    }
#endif
    return SUCC; // SUCCESS;
}



/**
 * @brief 发送激活请求
 * @return int32_t 0 - success; others - failure
 */
int32_t swei_srv_active(void)
{
    login_msg_st msg_st;
    uint8_t send_data[256];
    uint8_t send_len;
//    int32_t     result;

    /* 1.封装消息 */
    send_len = swei_package_protocol_msg(NULL, (uint8_t *)&msg_st, NET_PROTOCOL_ID_REQUEST_ACTIVATE, send_data);
    if (send_len <= 0)
    {
        log_e("tbox package request fail, send_len = %d", send_len);
        return -EIO;
    }

#if 0
    /* 3. 句柄获取到之后就发送消息, to-do:根据返回值检查发送结果  */
    result = swei_tcp_write(send_data, send_len);
    if (result <= 0)
    {       
        log_e("tbox send active msg fail, result:%d", result);
        return result;
    }
#else 
    swei_datatype_t dt = {0};

    dt.data_ptr = qlmalloc(send_len);
    if (NULL == dt.data_ptr) {
        log_e("active request malloc failed!\r\n");
        return -EPERM;
    }
    
    memcpy(dt.data_ptr, send_data, send_len);
    dt.data_len = send_len;
    dt.data_type = TYPE_ACTIVE_REQ;

    if (0 != swei_base_cache_push(&dt)) { 
        log_e("active request push to cache failed!\r\n");
        if (dt.data_ptr) {
            qlfree(dt.data_ptr);
        }
        
        return -EPERM;
    }
#endif
    return SUCC;
}





